package com.hk.admin.common.model.system;

import com.hk.admin.common.constant.GlobalConstants;
import com.hk.admin.common.constant.GlobalEnum;
import com.hk.admin.common.constant.system.UserEnum;
import com.hk.admin.common.dto.system.user.UserDTO;
import com.hk.admin.common.dto.system.user.UserSearchDTO;
import lombok.Data;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.*;
import javax.persistence.criteria.Predicate;
import java.io.Serializable;
import java.util.*;

@Entity
@Data
@Table(name = GlobalConstants.TABLE_PREFIX + "user")
public class UserModel implements Serializable {

    @Id
    @GeneratedValue
    private Long id;

    private String phoneNumber;

    private String userName;
    private String password;

    private String headPictureUrl;     // 头像

    @Column(updatable = false)
    private String IDCard;              // 身份证号码

    @Column(updatable = false)
    private Integer accountType;       // 帐号类型，0:平台，1：XXX，2：XXX，...
    private Integer accountStatus;     // 帐号状态，0：启用，1：禁用
    private Integer deleted;            // 删除状态，0：否，1：是

    @Column(updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date createTime;        // 创建时间
    @Temporal(TemporalType.TIMESTAMP)
    private Date updateTime;        // 修改时间

    private Integer areaId;         // 区域
    private String areaName;
    private String address;         // 详细地址

    private Integer superAdmin;     // 超级管理员，0：否，1：是

    @JoinTable(name = GlobalConstants.TABLE_PREFIX + "user_role", joinColumns = {@JoinColumn(name = "user_id")}, inverseJoinColumns = {@JoinColumn(name = "role_id")})
    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
    private List<RoleModel> roles;

    public UserModel() {
    }

    public UserModel(UserDTO dto) {
        this.userName = dto.getUserName();
        this.phoneNumber = dto.getPhoneNumber();
        this.accountType = dto.getAccountType();
        this.areaId = dto.getAreaId();
        this.address = dto.getAddress();

        this.headPictureUrl = dto.getHeadPictureUrl();

        this.accountStatus = UserEnum.ACCOUNT_STATUS.ENABLE.getKey();
        this.deleted = GlobalEnum.DELETED.NO.getKey();
        this.createTime = new Date();
    }


    /**
     * 人员列表
     * 创建JPA动态查询条件
     */
    public static Specification<UserModel> createSpecification(UserSearchDTO dto) {
        return (root, cq, cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (dto.getUserName() != null && !"".equals(dto.getUserName())) {
                predicates.add(cb.like(root.get("userName"), "%" + dto.getUserName()));
            }
            predicates.add(cb.equal(root.get("deleted"), GlobalEnum.DELETED.NO.getKey()));
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        };
    }

    /**
     * 判断是否是超级管理员
     */
    public boolean iAmASuperAdmin() {
        return this.superAdmin != null && this.superAdmin.equals(UserEnum.SUPER_ADMIN.YES.getKey());
    }

    /**
     * 更新
     */
    public void update(UserDTO dto) {
        this.userName = dto.getUserName();
        this.phoneNumber = dto.getPhoneNumber();
        this.areaId = dto.getAreaId();
        this.address = dto.getAddress();
        this.headPictureUrl = dto.getHeadPictureUrl();

        this.accountStatus = dto.getAccountStatus();
        this.updateTime = new Date();
    }

    /**
     * 删除
     */
    public void delete() {
        this.deleted = GlobalEnum.DELETED.YES.getKey();
        this.updateTime = new Date();
    }

}
